package com.mercenary.wein.screenControl.ui.batch;

import com.mercenary.wein.screenControl.command.CommandTool;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.Border;

public class LocationOperation extends JDialog implements ActionListener, ItemListener {
    GroupCheckBoxPanel LocationJ1 = new GroupCheckBoxPanel();//面板1
    JPanel LocationJ2 = new JPanel();//面板2
    JPanel LocationJ3 = new JPanel();//面板2上的搜索面板
    JPanel LocationJ9 = new JPanel();//面板2上的地图面板
    JPanel LocationJ10 = new JPanel();//面板2上的按钮面板
    static JFrame locationjf = new JFrame();
    String location;

    public LocationOperation() {
        super(locationjf, "站街");
        setBounds(200, 100, 700, 550);
        setLayout(new BorderLayout());
        setVisible(true);
        add(LocationJ1, BorderLayout.WEST);
        add(LocationJ2, BorderLayout.EAST);
        LocationJ1.setPreferredSize(new Dimension(180, 550));//设置大小
        LocationJ1.setBackground(Color.WHITE);
        LocationJ2.setPreferredSize(new Dimension(500, 550));
        LocationJ2.setBackground(Color.WHITE);

        Border titleBorder1 = BorderFactory.createTitledBorder("请选择在线设备");// 设置面板边框，实现分组框的效果       .设置面板边框颜色
        LocationJ1.setBorder(titleBorder1);

        //J2中的组件
        LocationJ2.setLayout(new BorderLayout());
        LocationJ2.add(LocationJ3, BorderLayout.NORTH);
        LocationJ2.add(LocationJ9, BorderLayout.CENTER);
        LocationJ2.add(LocationJ10, BorderLayout.SOUTH);
        JButton bb = new JButton("打开百度地图");
        // JTextField  jTextField=new JTextField();

        final JTextField jTextField = new JTextField();
        jTextField.setPreferredSize(new Dimension(200, 50));
        JButton execute = new JButton("执行定位");
        final JButton locationsearch = new JButton("站街操作");
        LocationJ3.add(bb);
        LocationJ3.add(jTextField);
        LocationJ3.add(execute);
        LocationJ3.add(locationsearch);
        LocationJ2.validate();
        bb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Desktop desktop = Desktop.getDesktop();
                try {
                    desktop.browse(new URI("http://api.map.baidu.com/lbsapi/getpoint/index.html"));
                } catch (IOException ee) {
                    // TODO Auto-generated catch block
                    ee.printStackTrace();
                } catch (URISyntaxException ee) {
                    ee.printStackTrace();
                }
            }
        });
        execute.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                location = jTextField.getText();
                String[] loc = location.split(",");
                if (location.length() <= 0) {
                    JOptionPane.showMessageDialog(LocationJ2, "请先执行定位");
                }else if(loc.length!=2){
                    JOptionPane.showMessageDialog(LocationJ2, "经纬度格式错误，请填写逗号分隔的经纬度信息");
                }else if(!loc[0].matches("[\\d\\.]+")||!loc[1].matches("[\\d\\.]+")){
                    JOptionPane.showMessageDialog(LocationJ2, "经纬度格式错误，经纬度应该是数字");
                }
                CommandTool.simulation(LocationJ1.getSelectedDevices(),
                        Double.parseDouble(location.split(",")[0])
                        ,Double.parseDouble(location.split(",")[1]));
            }
        });

    }


    public static void main(String[] arge) {
        new LocationOperation();


    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub

    }
}

